﻿using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

namespace ZTools
{
    [RequireComponent(typeof(PageLoadingModel))]
    public abstract class PageLoading<T> : MonoBehaviour
    {
        /// <summary>
        /// 手动获取当前选择那页
        /// </summary>
        public int CrtPageNum { get { return pageLoadingController.indexPage; } }
        public PageLoadingController pageLoadingController;
        [HideInInspector]
        public PageLoadingModel pageLoadingModel;
        /// <summary>
        /// 记录每一页的信息
        /// </summary>
        private Dictionary<int, List<T>> pageItemDic = new Dictionary<int, List<T>>();
        public virtual void Start()
        {
            if (pageLoadingModel.awake2Init)
            {
                pageLoadingController.Init();
            }
            pageLoadingModel.Init();
        }
        /// <summary>
        /// 初始化分页加载
        /// </summary>
        public void Init()
        {
            pageLoadingModel = this.GetComponent<PageLoadingModel>();
            pageLoadingController = new PageLoadingController
            {
                pageLoadingModel = this.pageLoadingModel,
                //pageLoadingView = this
            };
            pageLoadingController.onHttpDataRequest.AddListener((val) =>
            {
                GetData(val);
            });
            pageLoadingController.onRefreshItemData.AddListener(SetItem);
            pageLoadingModel.pageLoadingController = pageLoadingController;
            pageItemDic.Clear();
            pageLoadingController.Init();
        }
        /// <summary>
        /// 刷新分页加载
        /// </summary>
        public void Refresh()
        {
            Init();
        }
        /// <summary>
        /// 跳到对应的页面
        /// </summary>
        public void Jump2Page(int page)
        {
            pageLoadingController.Click2Page(page);
        }
        /// <summary>
        /// 通过网络请求获取当前页面数据
        /// </summary>
        /// <param name="indexPage">当前页码</param>
        /// <param name="pageNum">当前页请求数量</param>
        /// <param name="callBak">回调函数：（当前页数据对象list，总共有多少条数据）</param>
        public abstract void GetPageDataEvent(int indexPage, int pageNum, Action<List<T>, int> callBak);

        /// <summary>
        /// 设置显示每一条数据
        /// </summary>
        /// <param name="item">要显示的内容对象</param>
        /// <param name="itemData">显示数据</param>
        /// <param name="pageNum">当前是第几条内容</param>
        public abstract void SetPageItemEvent(Transform item, T itemData, int itemIndex);
        /// <summary>
        /// 调用接口获取要更新的数据
        /// </summary>
        /// <param name="indexPage">当前页码</param>
        /// <param name="pageNum">当前页请求数量</param>
        /// <param name="fun">回调函数（当前页数据对象list，总共有多少条数据）</param>
        public void GetData(int indexPage)
        {
            if (!pageItemDic.ContainsKey(indexPage) || pageLoadingModel.everyPageRequest)
            {
                if (pageItemDic.ContainsKey(indexPage))
                {
                    pageItemDic[indexPage].Clear();
                    pageItemDic.Remove(indexPage);
                }
                pageLoadingController.isLoading = true;
                GetPageDataEvent(indexPage, pageLoadingModel.pageItemNum, (List<T> dataList, int count) =>
                {
                    if (!pageItemDic.ContainsKey(indexPage))
                    {
                        pageItemDic.Add(indexPage, dataList);
                    }
                    pageLoadingController.UpdateData(count);
                });
            }
            else
            {
                pageLoadingController.UpdateData();
            }
        }
        /// <summary>
        /// 根据获得的对象设置应该显示的内容
        /// </summary>
        /// <param name="item"></param>
        /// <param name="data"></param>
        /// <param name="i"></param>
        public void SetItem()
        {
            for (int i = 0; i < pageItemDic[pageLoadingController.indexPage].Count; i++)
            {
                Transform item = pageLoadingModel.AddItemObj();
                SetPageItemEvent(item, pageItemDic[pageLoadingController.indexPage][i], i + 1);
            }
        }
        public virtual void OnDestroy()
        {
            pageLoadingController = null;
            GC.Collect();
        }
    }
}

